package jp.ac.u_tokyo.dch.util;

import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

public class TrimmingStringConverter extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		if (values != null && values.length > 0) {
			return DchCommonUtil.trimUni(values[0]);
		}

		return null;
	}

	@Override
	public String convertToString(Map context, Object o) {
		if (o != null) {
			return o.toString();
		} else {
			return null;
		}
	}

	@Override
	public Object convertValue(Map context, Object o, Class toClass) {
		if (o == null) {
			return null;
		} else if (toClass == java.lang.String.class) {
			if (o instanceof String[]) {
				String[] os = (String[]) o;

				if (os.length > 0) {
					return DchCommonUtil.trimUni(os[0]);
				}
			}

			return null;
		}

		return super.convertValue(context, o, toClass);
	}

}
